package com.yx.yinshanjinniu.adapters;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yx.yinshanjinniu.R;
import com.yx.yinshanjinniu.application.MyApplication;
import com.yx.yinshanjinniu.bean.PayTypeBean;
import com.yx.yinshanjinniu.core.AppAction;
import com.yx.yinshanjinniu.utils.GlideUtils;

import java.util.List;

/**
 * Created by Administrator on 2018/1/17 0017.
 */

public class PayTypeListAdapter extends CommonAdapter<PayTypeBean> {
    private int mCheckedPosition = 0;
    private AppAction appAction;

    public PayTypeListAdapter(Context context) {
        super(context);
        appAction = ((MyApplication) context.getApplicationContext()).getAppAction();
    }


    @Override
    public void setDataSource(List<PayTypeBean> list) {
        mDatas.clear();
        for (PayTypeBean bean : list) {
            mDatas.add(bean);
        }
        notifyDataSetChanged();
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = mInflater.inflate(R.layout.item_recharge_type, parent, false);
            holder.findViews(convertView);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.setData(mDatas.get(position), position);
        return convertView;
    }

    class ViewHolder {
        private ImageView iv_recharge_image;
        private TextView tv_recharge_type;
        private TextView tv_recharge_desc;
        private ImageView iv_check;


        private void findViews(View rootView) {
            iv_recharge_image = (ImageView) rootView.findViewById(R.id.iv_recharge_image);
            tv_recharge_type = (TextView) rootView.findViewById(R.id.tv_recharge_type);
            tv_recharge_desc = (TextView) rootView.findViewById(R.id.tv_recharge_desc);
            iv_check = (ImageView) rootView.findViewById(R.id.iv_check);
        }

        private void setData(PayTypeBean bean, int position) {
            GlideUtils.showImageOptions(mContext, bean.icon, iv_recharge_image);
            tv_recharge_type.setText(bean.title);
            tv_recharge_desc.setText(bean.tag);
            iv_check.setSelected(position == mCheckedPosition);
        }
    }

    public void setCheckedPosition(int mCheckedPosition) {
        this.mCheckedPosition = mCheckedPosition;
        notifyDataSetChanged();
    }

    public int getCheckedPosition() {
        return mCheckedPosition;
    }

    public int getCheckedType() {
        return mDatas.get(mCheckedPosition).type;
    }
}
